import { message } from "antd";
import Axios from "axios";
import Store from "../stores";


export var AxioInstance = Axios.create({
  proxy: {
    host: "127.0.0.1",
    port: 8081,
  },
  // baseURL: "http://192.168.1.235:3000/",
  timeout: 25000,
});
// AxioInstance.defaults.headers.common["Authorization"] =
//   "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpblR5cGUiOjMsInVzZXJfbmFtZSI6InpoYW5nZXJnYW5nIiwic2NvcGUiOlsiYWxsIl0sImV4cCI6MTYxODcxMTk5OCwidXNlcklkIjoyNiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9BRE1JTiJdLCJqdGkiOiJmY2Y0YjI3ZS01MzYyLTQwYWEtYmZiYy0zYTM2Zjg1NWYzMzkiLCJjbGllbnRfaWQiOiJ0ZXN0In0.3IhOhJfXv1ysARZbeS0_DosaqSseZMvl0GOD0sW3XqI";
//请求发出前拦截
AxioInstance.interceptors.request.use(
  function (config) {
    config.headers["Content-Type"] = "application/json;charset=UTF-8";
    if (Store.userStore.accessToken && Store.userStore.accessToken.length>0) {
      //已经登录的请求头配置，主要用于登录成功后后台返回的token,做权限
      config.headers["Authorization"] = "Bearer " + Store.userStore.accessToken;
    } else {
      // 未登录的请求头配置
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);
AxioInstance.interceptors.response.use(
  function (response) {
    if (response.data.access_token) {
      return Promise.resolve(response.data);
    } else {
      if (response.data.code === 10000) {
        return Promise.resolve(response.data.data);
      } else {
        return Promise.reject(response.data);
      }
    }
  },
  () => {
    message.error("请求错误");
    Promise.resolve({ data: undefined });
  }
);
